//
//  AddBuddyController.m
//  Cocoa-Fetion
//
//  Created by Ronnie on 08-12-21.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "AddBuddyController.h"
#import "MainAppController.h"

@implementation AddBuddyController

- (void)dealloc
{
	if (buddyList) {
		[buddyList release];
	}
	if (myFetionData) {
		[myFetionData release];
	}
	if (mainAppCtrl) {
		[mainAppCtrl release];
	}
	if (mainWindowCtrl) {
		[mainWindowCtrl release];
	}
    [super dealloc];
}

- (void)awakeFromNib
{
    NSLog(@"Add buddy window loaded");
	if ([sendTypeRadio selectedTag] == 8001 )
	{
		[fetionNumberField setEnabled:NO];
		[mobileNumberField setEnabled:YES];
	}
	else if([sendTypeRadio selectedTag] == 8002 )
	{
		[fetionNumberField setEnabled:YES];
		[mobileNumberField setEnabled:NO];
	}
}

- (void)setMainWindowController:(MainWindowController*)p
{
	NSLog(@"AddBuddy: set main window controller");
	mainWindowCtrl = [p retain];
}

- (void)setMainAppController:(MainAppController*)p
{
	
	NSLog(@"AddBuddy: set main app controller");
	mainAppCtrl = [p retain];
	myFetionData = [[mainAppCtrl getFxData] retain];
	buddyList = [[myFetionData getMyBuddyList] retain];
	[self setGroupListAndUserInfo];
}

- (void)setGroupListAndUserInfo
{
	[groupInfo removeAllItems];
	FXTreeNode* node;
	for (int i=0; i<[buddyList numberOfChildren]; i++) {
		node = [buddyList childAtIndex:i];
		if ([node isGroup]) {
			[groupInfo addItemWithObjectValue:[node getGroupName]];
			//[[groupInfo itemObjectValueAtIndex:i] setTag:[node groupId]];
		}
	}
	
	[userNameField setStringValue:[myFetionData getMyNickName]];
}

- (void)sendAddBuddyRequest
{
	if ([sendTypeRadio selectedTag] == 8001 )
	{
		NSString* buddyMobileNumber = [mobileNumberField stringValue];
		if ([buddyMobileNumber length]!=0) {
			NSString* showName = [buddyShowNameField stringValue];
			int row = [groupInfo indexOfSelectedItem];
			FXTreeNode* groupNode = [buddyList childAtIndex:row];
			NSString* descriptiong = [NSString stringWithFormat:@"我是%@，正在使用中中国移动飞信业务，相加你为好友。", [userNameField stringValue]?[userNameField stringValue]:[myFetionData getMyNickName]];
			NSLog(@"%@ with nick name %@ add to group %@", buddyMobileNumber, showName, [groupNode getGroupName]);
			fx_add_buddy_by_mobile([buddyMobileNumber UTF8String], [showName UTF8String], [groupNode groupId], [descriptiong UTF8String], nil, nil);
		}
	}
	else if([sendTypeRadio selectedTag] == 8002 )
	{
		NSString* buddyFetionNumber = [fetionNumberField stringValue];
		if ([buddyFetionNumber length]!=0) {
			NSString* showName = [buddyShowNameField stringValue];
			int row = [groupInfo indexOfSelectedItem];
			FXTreeNode* groupNode = [buddyList childAtIndex:row];
			NSString* descriptiong = [NSString stringWithFormat:@"我是%@，正在使用中中国移动飞信业务，相加你为好友。", [userNameField stringValue]?[userNameField stringValue]:[myFetionData getMyNickName]];
			NSLog(@"%@ with nick name %@ add to group %@", buddyFetionNumber, showName, [groupNode getGroupName]);
			fx_add_buddy_by_uid([buddyFetionNumber UTF8String], [showName UTF8String], [groupNode groupId], [descriptiong UTF8String], nil, nil);		
		}
	}
}

- (IBAction)sendTypeChanged:(id)sender
{
	if ([sendTypeRadio selectedTag] == 8001 )
	{
		[fetionNumberField setEnabled:NO];
		[mobileNumberField setEnabled:YES];
	}
	else if([sendTypeRadio selectedTag] == 8002 )
	{
		[fetionNumberField setEnabled:YES];
		[mobileNumberField setEnabled:NO];
	}
}

- (IBAction)groupInfoChanged:(id)sender
{
	int row = [groupInfo indexOfSelectedItem];
	NSLog(@"%i", row);
}

@end
